<?php

namespace app\setting\service\system;
//自定义页面
use app\common\model\RuinuoSetting as DataMode;

//当前模型
use think\facade\Db;

class Custom
{
    /*保存前处理*/
    public function savebefore($params = [], $id = 0)
    {
        $datas = $params['a'];
        $path = App()->getRootPath() . 'example.env';
        $example = file_get_contents($path); // 读取文件内容
        $pattern = "/'([^']*)'/";
        preg_match_all($pattern, $example, $matches);
        $arr = $matches[1];
        foreach ($arr as $k => $v) {
            $item = explode('.', $v);
            $field = $item[0] . '_' . $item[1];
            $value = !empty($datas[$field]) ? $datas[$field] : '';
            switch ($item[0]) {
                case "app":
                    $DEBUG = "false";
                    if ($value == 1) {
                        $DEBUG = "true";
                    }
                    $example = str_replace("'" . $v . "'", $DEBUG, $example);
                    break;
                case "database":
                    $example = str_replace("'" . $v . "'", env($v), $example);
                    break;
                case "redis":
                case "aliyun":
                case "qiniu":
                case "qcloud":
                    $example = str_replace("'" . $v . "'", $value, $example);
                    break;
            }
        }
        getCreatetxt(App()->getRootPath() . '.env', $example);
        return $params;
    }

    /*保存后处理*/
    public function saveafter($id = 0)
    {


    }

    /*单条查询后处理*/
    public function oneafter($params = [])
    {
        return $params;
    }

}